﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using SYH.System.Model;
using SYH.Utils;

namespace admin.syh876.com.Code
{
    public class LoginAdmin
    {
        #region 从cookie中得到用户信息
        /// <summary>
        /// 从cookie中得到用户信息
        /// </summary>
        /// <param name="objAdminUser"></param>
        public static void GetLoginByCookie(ref AdminUser objAdminUser)
        {
            try
            {
                if (objAdminUser == null)
                {
                    objAdminUser = new AdminUser();
                    objAdminUser.LoginResult = false;
                }

                string urlreferer = HttpContext.Current.Request.Url.LocalPath;
                string cookieName = FormsAuthentication.FormsCookieName;
                HttpCookie uCookie = HttpContext.Current.Request.Cookies["adminInfo"];
                if (uCookie != null)
                {

                    string strUid = (uCookie.Values.Get("ui") != null ? uCookie.Values.Get("ui") : "").Trim().Replace("'", "");
                    string strUpsd = (uCookie.Values.Get("up") != null ? uCookie.Values.Get("up") : "").Trim().Replace("'", "");
                    string strUTime = (uCookie.Values.Get("utm") != null ? uCookie.Values.Get("utm") : "").Trim().Replace("'", "");

                    strUid = Encrypt.TripleDESDecrypting(strUid);
                    strUpsd = Encrypt.TripleDESDecrypting(strUpsd);
                    strUTime = Encrypt.TripleDESDecrypting(strUTime);

                    if (!IsDateTime(strUTime))
                    {
                        objAdminUser.LoginResult = false;
                        return;
                    }
                    else
                    {
                        //防止伪造cookies
                        TimeSpan ts = DateTime.Now.Subtract(Convert.ToDateTime(strUTime));
                        //36000000000   ticks 60分钟
                        if (ts.Ticks > 36000000000)
                        {
                            objAdminUser.LoginResult = false;
                            return;
                        }
                    }

                    LogOnModel model = new LogOnModel();
                    model.UserName = strUid;
                    model.PassWord = strUpsd;
                    objAdminUser = GetAdminUser(model);
                }
                else
                {
                    objAdminUser.LoginResult = false;
                }
            }
            catch (Exception ex)
            {
                objAdminUser.LoginResult = false;
            }
        }
        #endregion

        #region 登陆用户信息
        /// <summary>
        /// 登陆用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static AdminUser GetAdminUser(LogOnModel model)
        {
            AdminUser objAdminUser = new AdminUser();
            objAdminUser.LoginResult = false;

            SystemAdmin model_SystemAdmin = SYH.System.BLL.SystemAdmin.GetModel(model.UserName, model.PassWord);

            if (model_SystemAdmin != null)
            {
                SystemRole objSystemRole = SYH.System.BLL.SystemRole.GetModel(model_SystemAdmin.RoleId);
                if (objSystemRole != null)
                {
                    objAdminUser.ID = model_SystemAdmin.Id;
                    objAdminUser.UserName = model_SystemAdmin.UserName;
                    objAdminUser.LoginPassword = model_SystemAdmin.PassWord;
                    objAdminUser.RoleId = model_SystemAdmin.RoleId;
                    objAdminUser.RoleName = objSystemRole.RoleName;
                    objAdminUser.ActionCollection = objSystemRole.ActionCollection;
                    objAdminUser.LoginResult = true;

                    HttpCookie objCookie = new HttpCookie("adminInfo");
                    objCookie.Values.Add("ui", Encrypt.TripleDESEncrypting(objAdminUser.UserName));
                    objCookie.Values.Add("up", Encrypt.TripleDESEncrypting(objAdminUser.LoginPassword));
                    objCookie.Values.Add("utm", Encrypt.TripleDESEncrypting(DateTime.Now.ToString()));
                    objCookie.Expires = DateTime.Now.AddMinutes(60);
                    objCookie.Path = "/";
                    objCookie.HttpOnly = true;
                    objCookie.Secure = false;
                    HttpContext.Current.Response.Cookies.Set(objCookie);
                }
            }

            return objAdminUser;
        }
        #endregion

        #region 判断时间
        /// <summary>
        /// 验证字符型日期
        /// </summary>
        /// <param name="strDateTime">字符串日期</param>
        /// <returns></returns>
        public static bool IsDateTime(string strDateTime)
        {
            DateTime dt = DateTime.Now;
            bool result = DateTime.TryParse(strDateTime, out dt);
            return result;
        }
        #endregion
    }
}